package services

import (
	"awesome-devops/applications/rpc/define"
	"awesome-devops/applications/tools"
	"context"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
)

type NodeResource struct {
	define.UnimplementedNodeServiceServer
}

func (nodeResource *NodeResource) GetNodeResource(context context.Context, request *define.NodeInfoRequest) (*define.NodeInfoResponse, error) {
	defer func() {
		if err := recover(); err != nil {
			tools.GetLogger().Error("rpcError:" + commonhelper.PrintStackTrace(err))
			return
		}
	}()
	data := tools.GetKernelDriver().GetNodeCpuUseAndMemory()
	response := define.NodeInfoResponse{}
	response.Code = 0
	response.Message = "success"
	response.MemoryTotal = int64(data["totalMemory"].(uint64))
	response.SurplusMemory = int64(data["freeMemory"].(uint64))
	response.Cpu = int32(data["cpu"].(int))
	response.CpuSurplus = int32(data["cpu"].(int))
	return &response, nil
}
